package com.cooper.scraper.show;

import java.util.Map.Entry;

import com.cooper.android.comm.LRULinkedHashMap.RemoveEldestEntryListener;
import com.cooper.android.comm.LRULinkedHashMap;
import com.cooper.android.comm.Resource;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

public abstract class FanartViewStrategy<T extends Resource> implements
		RemoveEldestEntryListener<String, Bitmap> {

	protected LRULinkedHashMap<String, Bitmap> cache = null;

	protected FanartImgStrategy fanartImgStrategy;

	protected LayoutParams viewLayoutParams;

	public static final int WAIT_VIEW_TYPE = 0;
	public static final int FANART_VIEW_TYPE = 1;

	public abstract Bitmap getMediaFanart(T media);

	public void onReturnView(FanartViewHolder<T> holder, int type) {
	}

	public void onConvertView(FanartViewHolder<T> holder) {
	}

	public FanartImgStrategy getFanartImgStrategy() {
		return fanartImgStrategy;
	}

	public void setFanartImgStrategy(FanartImgStrategy fanartImgStrategy) {
		this.fanartImgStrategy = fanartImgStrategy;
	}

	public LayoutParams getViewLayoutParams() {
		return viewLayoutParams;
	}

	public void setViewLayoutParams(LayoutParams viewLayoutParams) {
		this.viewLayoutParams = viewLayoutParams;
	}

	public void init(int showCount) {
		if (cache != null) {
			cache.clear();
		}
		cache = new LRULinkedHashMap<String, Bitmap>(showCount * 3);
		cache.setRemoveEldestEntryListener(this);
	}

	public void destroy() {
		reset();
		waitingImg = null;
	}

	public void reset() {
		if (cache != null)
			cache.clear();
	}

	public void configurationChanged(Configuration configuration,
			Display display, int showCount) {
		reset();
		cache = new LRULinkedHashMap<String, Bitmap>(showCount * 5);
	}

	@Override
	public boolean onRemove(Entry<String, Bitmap> eldest) {
		Bitmap img = eldest.getValue();
		if (img != null && !img.isRecycled()) {
			img.recycle();
		}
		return true;
	}

	private int watingImagRes;

	public int getWatingImagRes() {
		return watingImagRes;
	}

	public void setWatingImagRes(int watingImagRes) {
		this.watingImagRes = watingImagRes;
	}

	@SuppressWarnings("unchecked")
	public final View getResourceView(Context context, T media,
			View convertView, ViewGroup parent) {
		FanartViewHolder<T> holder;
		if (convertView != null) {
			holder = (FanartViewHolder<T>) convertView.getTag();
			onConvertView(holder);
		} else {
			holder = new SingleImgViewHolder<T>(context);
		}
		int viewType = FanartViewStrategy.FANART_VIEW_TYPE;

		Bitmap result = getMediaFanart(media);
		if (result == null) {
			result = getWaitingImg(context);
			viewType = FanartViewStrategy.WAIT_VIEW_TYPE;
		}

		holder.setFanart(result);
		holder.getRootView().setLayoutParams(viewLayoutParams);
		holder.setResource(media);
		onReturnView(holder, viewType);
		return holder.getRootView();
	}

	private Bitmap waitingImg = null;

	private Bitmap getWaitingImg(Context context) {
		if (waitingImg == null) {

			Bitmap fanartImg = BitmapFactory.decodeResource(
					context.getResources(), getWatingImagRes());
			waitingImg = fanartImgStrategy.handleFanartImg(context, Bitmap
					.createScaledBitmap(fanartImg,
							(int) fanartImgStrategy.getFanartWidth(),
							(int) fanartImgStrategy.getFanartHeight(), true));
			if (fanartImg != null && !fanartImg.isRecycled()) {
				fanartImg.recycle();
			}
		}
		return waitingImg;
	}

}
